﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace LightCAD.MathLib.CSG
{
   class BBox2
    {
        Vec2 minp, maxp;
        public BBox2()
        {
            minp = new Vec2(double.MaxValue, double.MaxValue);
            maxp = new Vec2(-double.MaxValue, -double.MaxValue);
        }
        public BBox2(Vec2 minp, Vec2 maxp)
        {
            this.minp = minp.clone();
            this.maxp = maxp.clone();
        }
        public BBox2(BBox2 box)
        {
            this.minp = box.minp.clone();
            this.maxp = box.maxp.clone();
        }

        [MethodImpl((MethodImplOptions)768)]
        public bool isEmpty()
        {
            return this.maxp.x < this.minp.x || this.maxp.y < this.minp.y;
        }
        public Vec2 dim()
        {
            return this.maxp - this.minp;
        }
        public double perimeter()
        {
            Vec2 d = dim();
            return 2 * (d.x + d.y);
        }

        public override string ToString()
        {
            return $"(min:{this.minp}, max:{this.maxp})";
        }


        [MethodImpl((MethodImplOptions)768)]
        public static bool isIn(Vec2 p, BBox2 bb)
        {
            return bb.minp.x <= p.x && p.x <= bb.maxp.x &&
                   bb.minp.y <= p.y && p.y <= bb.maxp.y;
        }

        public static bool hasIsct(BBox2 lhs, BBox2 rhs)
        {
            return lhs.minp.x <= rhs.maxp.x && lhs.maxp.x >= rhs.minp.x &&
                   lhs.minp.y <= rhs.maxp.y && lhs.maxp.y >= rhs.minp.y;
        }

        public static BBox2 convex(BBox2 lhs, BBox2 rhs)
        {
            return new BBox2(Vec2.min(lhs.minp, rhs.minp), Vec2.max(lhs.maxp, rhs.maxp));
        }
        public static BBox2 isct(BBox2 lhs, BBox2 rhs)
        {
            return new BBox2(Vec2.max(lhs.minp, rhs.minp), Vec2.min(lhs.maxp, rhs.maxp));
        }


    }
}
